package com.hspedu.object_;

public class ToString {
    public static void main(String[] args) {
      /*
         Object 的toString() 源码
         (1) getClass().getName() 全类名(包名+类名)
         (2) Integer.toHexString(hashCode()) 将对象的hashCode值转成16进制字符串
         public String toString() {
            return getClass().getName() + "@" + Integer.toHexString(hashCode());
         }
        }*/

        Monster monster = new Monster("小妖怪", "巡山", 1000);
        System.out.println(monster.toString() +  " hashcode=" +monster.hashCode());
        //com.hspedu.object_.Monster@49e4cb85
        System.out.println("==当直接输出一个对象时，toString 方法会被默认调用==");
        System.out.println(monster);
    }
}

class Monster{
    private String name;
    private String job;
    private double sal;

    //重写toString方法，输出对象的属性
    //使用快捷键 alt+inset -> toString

    @Override
    public String toString() {//重写后，一般是把对象的属性输出，当然程序员也可以自己定制
        return "Monster{" +
                "name='" + name + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                '}';
    }

    public Monster(String name, String job, double sal) {
        this.name = name;
        this.job = job;
        this.sal = sal;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public double getSal() {
        return sal;
    }

    public void setSal(double sal) {
        this.sal = sal;
    }
}

